//
//  MapCarSafeRange.m
//  carcareIOS
//
//  Created by ileo on 15/7/6.
//  Copyright (c) 2015年 baozun. All rights reserved.
//

#import "MapCarFence.h"
#import "LocAnnotation.h"
#import "LocAnnotationView.h"
#import "MapReGeocodeSimple.h"
#import "StrokeLabel.h"
#import <MapKit/MKGeometry.h>

@interface MapCarFence()<MAMapViewDelegate>

@property (nonatomic, strong) MapReGeocodeSimple *mapSimple;

@end

@implementation MapCarFence

- (instancetype)init
{
    self = [super init];
    if (self) {

        self.mapView = [[MAMapView alloc] initWithFrame:CGRectMake(0, 0, 10, 10)];
        self.mapView.delegate = self;
        
        self.mapView.zoomLevel = 12;
        self.mapView.scrollEnabled = YES;
        self.mapView.zoomEnabled = NO;
        self.mapSimple = [[MapReGeocodeSimple alloc] init];
        
    }
    return self;
}

-(void)showLocation{
    [self.mapView setCenterCoordinate:self.coordinate animated:YES];
}

-(void)showCircleWithCoordinate:(CLLocationCoordinate2D)coordinate radius:(CLLocationDistance)radius{
    self.coordinate = coordinate;
    self.radius = radius;
    
    LocAnnotation *annotation = [[LocAnnotation alloc] initWithCoordinate:coordinate];
    [self.mapView addAnnotation:annotation];
    __weak __typeof(self) wself = self;
    
    [self.mapSimple reGeoCodeCoordinate:coordinate finish:^(NSString *poi, AMapReGeocode *reGeocode) {
        if (wself.Address) {
            wself.Address(poi);
        }
    } error:nil];
}

-(MAOverlayRenderer *)mapView:(MAMapView *)mapView rendererForOverlay:(id<MAOverlay>)overlay{
    
    MACircleRenderer *circleRenderer = [[MACircleRenderer alloc] initWithCircle:(MACircle *)overlay];
    
//    circleRenderer.lineWidth   = 4.f;
//    circleRenderer.strokeColor = [UIColor blueColor];
    circleRenderer.fillColor   = [UIColor clearColor];
 
    MAMapRect rect = circleRenderer.circle.boundingMapRect;
    double gap = rect.size.width * 2 / 3;
    rect.size.width += gap;
    rect.size.height += gap;
    rect.origin.x -= gap / 2;
    rect.origin.y -= gap/2;

    [self.mapView setVisibleMapRect:rect animated:YES];
    
    return circleRenderer;
}

-(MAAnnotationView *)mapView:(MAMapView *)mapView viewForAnnotation:(id<MAAnnotation>)annotation{
    if ([annotation isKindOfClass:[LocAnnotation class]]) {
        
        LocAnnotationView *annotationView = [[LocAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"carAnnotation"];
        annotationView.image = ImageNamed(@"fence_address");
        
//
//        StrokeLabel *label = [[StrokeLabel alloc] initWithFrame:CGRectMake(0, 0, 100, 40)];
//        label.textAlignment = NSTextAlignmentCenter;
//        label.backgroundColor = [UIColor clearColor];
//        label.numberOfLines = 0;
//        label.font = FONT_DEFAULT_Light(15);
//        label.text = [NSString stringWithFormat:@"%0.0f米",self.radius];
//        label.textColor = [Theme mainColor];
//        
//        annotationView.calloutView = label;
//        __weak __typeof(self) wself = self;
//        annotationView.CalloutViewUpdate = ^(UIView *calloutView, id<MAAnnotation> annotation){
//            UILabel *label = (UILabel *)calloutView;
//            label.text = [NSString stringWithFormat:@"%0.0f米",wself.radius];
//        };
//        [annotationView setSelected:YES];
        
        return annotationView;
        
    }
    return nil;
}

-(void)setRadius:(CLLocationDistance)radius{
    _radius = radius;
    MACircle *old;
    for (id<MAOverlay> overlay in self.mapView.overlays) {
        if ([overlay isKindOfClass:[MACircle class]]) {
            old = (MACircle *)overlay;
            break;
        }
    }
    MACircle *circle = [MACircle circleWithCenterCoordinate:self.coordinate radius:radius];
    [self.mapView addOverlay:circle];
    if (old) {
        [self.mapView removeOverlay:old];
    }
    
    for (id<MAAnnotation> annot in self.mapView.annotations) {
        if ([annot isKindOfClass:[LocAnnotation class]]) {
            [self.mapView removeAnnotation:annot];
        }
    }
    LocAnnotation *annotation = [[LocAnnotation alloc] initWithCoordinate:self.coordinate];
    [self.mapView addAnnotation:annotation];
}
#pragma mark - 点击活着拖地图
- (void)mapView:(MAMapView *)mapView regionWillChangeAnimated:(BOOL)animated
{
    if(self.mapTouchBegin) self.mapTouchBegin();
}
- (void)mapView:(MAMapView *)mapView didSingleTappedAtCoordinate:(CLLocationCoordinate2D)coordinate
{
    if(self.mapTouchBegin) self.mapTouchBegin();
}

-(void)mapView:(MAMapView *)mapView regionDidChangeAnimated:(BOOL)animated{
    if (!animated) {
        self.coordinate = mapView.centerCoordinate;
        [self showCircleWithCoordinate:self.coordinate radius:self.radius];
        if (self.CoordinateChange) {
            self.CoordinateChange();
        }
    }
}




@end
